﻿using System;
using System.Linq;
using System.Web.Mvc;
using QDT.Common;
using QDT.Core.Deals;
using QDT.Core.Members;
using QDT.Core.Projects;
using QDT.MVC.Security;
using QDT.Services.Members;
using QDT.Web.Code;
using QDT.Web.Code.Enums;

namespace QDT.Web.Controllers
{
    [DefaultAuthorize]
    public class OfflineController : BaseController
    {
        private readonly IUserService _userService;

        protected new User User { get { return QDTContext.Current.User; } }

        public OfflineController(IUserService userService)
        {
            _userService = userService;
            if (User.GroupID != 1) throw new Exception("无效连接");
        }

        public ActionResult Project(int id)
        {
            var project = DbProvider.D<Project>().FirstOrDefault(x => x.ID == id);
            if (project == null) return NotFound();
            if (!project.IsEffect || project.IsDelete) return NotFound();
            return View(project);
        }

        public ActionResult UserCenter()
        {
            User.UserSta = _userService.UserSta(User.UserName);
            return View(User);
        }

        public ActionResult LogList(int? page)
        {
            const int pageSize = 10;
            var pageNumber = page ?? 1;

            var source = DbProvider.D<UserTransLog>().Where(x => x.UserID == User.ID)
                                   .OrderByDescending(x => x.ID);
            return View(source.ToPagedList(pageNumber - 1, pageSize));
        }

        public ActionResult InvestList(int? page)
        {
            const int pageSize = 10;
            var pageNumber = page ?? 1;

            var credits = DbProvider.D<Credit>().Where(x => x.UserID == User.ID && x.Money > 0)
                                   .OrderByDescending(x => x.ID);

            ViewBag.Route = InvestRoute.AllInvestList;

            return View(credits.ToPagedList(pageNumber - 1, pageSize));
        }

        public ActionResult ProjectList(int? page)
        {
            const int pageSize = 10;
            var pageNumber = page ?? 1;

            var credits = DbProvider.D<Project>().Where(x => x.IsEffect && !x.IsDelete && x.InnerStatus==(int)ProjectStatus.Progress)
                                   .OrderByDescending(x => x.ID);

            ViewBag.Route = InvestRoute.AllInvestList;

            return View(credits.ToPagedList(pageNumber - 1, pageSize));
        }

        public ActionResult ChangePassword()
        {
            return View(User);
        }

        public ActionResult PostPassword(string oldPass, string newPass, string confirm)
        {
            if (!ModelState.IsValid) return Error();

            User user = _userService.GetUserByID(User.ID);
            bool isChange = false;
            if (!string.IsNullOrEmpty(newPass))
            {
                if (newPass != confirm)
                {
                    return Error("两次密码不相同");
                }
                if (oldPass.Hash().Equals(user.Password))
                {
                    user.ChangePassword(newPass);
                    isChange = true;
                }
            }
            if (isChange)
            {
                _userService.UpdateUser(user);
                return Success("账户修改成功");
            }
            return Error("原始密码不正确");
        }

    }
}
